//
//  DYZLoginViewController.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZLoginViewController.h"
#import "UILabel+Addition.h"
#import "UIButton+Addition.h"
#import "UIColor+Addition.h"
#import "DYZForgetPsdController.h"
#import "DYZRegistePagesController.h"
#import "DYZLoginUserInfo.h"

#define kfilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:@"usersInfo.data"]
@interface DYZLoginViewController ()
//账户名
@property (nonatomic, weak)UITextField  *nameField;
//密码
@property (nonatomic, weak)UITextField *psdTextFidle;
//记住密码
@property (nonatomic, weak)UIButton *checkbox;

//记住密码的状态
@property (nonatomic, assign)BOOL  isRemember;

@property (nonatomic, strong)NSMutableArray *userInfoData;
@end

@implementation DYZLoginViewController

- (void)viewDidLoad {
    self.navigationItem.title = @"登录";
    
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    解档
    self.userInfoData = [NSKeyedUnarchiver unarchiveObjectWithFile:kfilePath];
}
- (void)setupUI {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithHex:0xebeefe];

//    图标
    UIImageView *iconView = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"dl_logo"];
    iconView.image = image;
    iconView.contentMode = UIViewContentModeCenter;
    [self.view addSubview:iconView];

//    账户名textfield
    UITextField *nameField = [[UITextField alloc] init];
    nameField.backgroundColor = [UIColor whiteColor];
    nameField.placeholder = @"用户名或手机号";
    nameField.keyboardType = UIKeyboardTypeNumberPad;
    self.nameField = nameField;
    UIView *nameView = [[UIView alloc] init];
    nameView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:nameView];
    [nameView addSubview:nameField];
//    密码textfield
    UITextField *psdTextFidle = [[UITextField alloc] init];
    psdTextFidle.placeholder = @"密码";
    psdTextFidle.backgroundColor = [UIColor whiteColor];
    psdTextFidle.secureTextEntry = YES;

    self.psdTextFidle = psdTextFidle;
    UIView *psdView = [[UIView alloc] init];
    psdView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:psdView];
    [psdView addSubview:psdTextFidle];
    
// 登录按钮
    UIButton *loginBtn = [UIButton buttonWithText:@"登录" andTextColor:[UIColor whiteColor] backgroundColor:[UIColor colorWithHex:0x0073dd] andFontSize:20];
    [self.view addSubview:loginBtn];


    //   注册页面

    UIButton *registerBtn = [UIButton buttonWithText:@"快速注册" andTextColor:[UIColor colorWithHex:0x666666] backgroundColor:[UIColor colorWithHex:0xebeefe] andFontSize:17];
    [registerBtn addTarget:self action:@selector(quickRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    
//    忘记密码按钮
    UIButton *memeryBtn = [UIButton buttonWithText:@"忘记密码" andTextColor:[UIColor colorWithHex:0x0073dd] backgroundColor:[UIColor colorWithHex:0xebeefe] andFontSize:17];
    [memeryBtn addTarget:self action:@selector(findOldPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:memeryBtn];

    [loginBtn addTarget:self action:@selector(loginSucess:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
//    自动布局
//    图标
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(21);
        make.left.offset(15);
        make.right.offset(15);
        make.height.offset(200);
    }];
    
    [nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(iconView.mas_bottom).offset(12);
        make.height.offset(45);
    }];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(15);
        make.top.bottom.offset(0);
    }];
//    密码
    [psdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(nameView.mas_bottom).offset(2);
        make.height.offset(45);
    }];
    [psdTextFidle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(15);
        make.top.bottom.offset(0);
    }];
    
//    登录按钮
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(psdTextFidle.mas_bottom).offset(16);
        make.left.offset(15);
        make.right.offset(-15);
        make.height.offset(60);
    }];

//    注册页面按钮
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(loginBtn);
        make.top.equalTo(loginBtn.mas_bottom).offset(15);
    }];
    
//    忘记密码按钮
    [memeryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(loginBtn);
        make.centerY.equalTo(registerBtn);
    }];

}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}
//密码判断
- (void)loginSucess:(UIButton *)sender {
    
    for (DYZLoginUserInfo *userInfo in self.userInfoData) {
        if ([self.nameField.text isEqualToString:userInfo.username] && [self.psdTextFidle.text isEqualToString:userInfo.password]) {
            
            [UIApplication sharedApplication].keyWindow.rootViewController = [[NSClassFromString(@"DYZTabbarController") alloc] init];
        } else {
            [self loginAlertWithTip:@"用户名或密码错误"];
            return;
        }
//    if ([self.nameField.text isEqualToString:@"123"] && [self.psdTextFidle.text isEqualToString:@"123"]) {
//        
//            [UIApplication sharedApplication].keyWindow.rootViewController = [[NSClassFromString(@"DYZTabbarController") alloc] init];
//    }else {
//        [self loginAlertWithTip:@"用户名或密码错误"];
//        return;
    }
}
//弹出警告
- (void)loginAlertWithTip:(NSString *)tip {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:tip preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:true completion:nil];
}
//忘记密码
- (void)findOldPassword {
    NSLog(@"忘记密码");
    DYZForgetPsdController *vc = [[DYZForgetPsdController alloc] init];
    [self.navigationController pushViewController:vc animated:true];

}

//快速注册
- (void)quickRegister {
    DYZRegistePagesController *vc = [[DYZRegistePagesController alloc] init];
    [self.navigationController pushViewController:vc animated:true];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@end
